<?php
class PersonasController extends AppController {

	var $name = 'Personas';
	var $uses= array('Persona');
	var $components= array('RequestHandler', 'Cookie');
	
	function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->autoRedirect = false;
		$this->Auth->authorize = 'crud';
		$this->Auth->allowedActions = array('login', 'logout');//Descomentar esta linea para las acciones a las cuales queremos dar libre acceso. (Usar * para dar acceso a todas las acciones.)
	}

	function index() {
		$this->Persona->recursive = 0;
		$this->set('personas', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid persona', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('persona', $this->Persona->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Persona->create();
			if ($this->Persona->save($this->data)) {
				$this->Session->setFlash(__('The persona has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The persona could not be saved. Please, try again.', true));
			}
		}
		$this->Persona->Empresa->fields = 'ci_rif, nit, nombre';
		$empresas = $this->Persona->Empresa->find('list', array('conditions'=>'empresa_id is not null', 'order'=>'nombre'));
		$medioPublicitario = $this->Persona->MedioPublicitario->find('list', array('order'=>'nombre'));
		$tipoPersonas = $this->Persona->TipoPersona->find('list');
		$this->set(compact('empresas', 'medioPublicitario', 'tipoPersonas'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid persona', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Persona->save($this->data)) {
				$this->Session->setFlash(__('The persona has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The persona could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Persona->read(null, $id);
		}
		$empresas = $this->Persona->Empresa->find('list');
		$medioPublicitarios = $this->Persona->MedioPublicitario->find('list');
		$usuarios = $this->Persona->Usuario->find('list');
		$grupos = $this->Persona->Grupo->find('list');
		$tipoPersonas = $this->Persona->TipoPersona->find('list');
		$this->set(compact('empresas', 'medioPublicitarios', 'usuarios', 'grupos', 'tipoPersonas'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for persona', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Persona->delete($id)) {
			$this->Session->setFlash(__('Persona deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Persona was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	//funciones para el logueo de usuarios, manejo de permisos, mediante Acl
	
	function login(){
		//$this->Auth->user('tipo_persona_id');
		if(!empty($this->data)){
			$this->Auth->__setDefaults();
			$this->Auth->_loggedIn = false;
	
			if ($user = $this->Auth->identify($this->data)) {
				$this->Session->setFlash('Usted ha iniciado sesión.');
				$this->Session->write($this->Auth->sessionKey, $user);
				$this->Auth->_loggedIn = true;
			}else{
				$this->Session->setFlash('Lo siento, la combinación usuario/clave es incorrecta.');
			}
			$this->redirect($this->Auth->redirect());
		}
		
	}
	
	
	function logout() {
		$this->Session->setFlash('La sesión ha sido cerrada.');
		$this->redirect($this->Auth->logout());
		
	}
}
?>